package io.github.eiot.json.deserializer;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import io.github.eiot.annotation.FrameField;
import io.github.eiot.codec.BCDNumber;

import java.io.IOException;

/**
 * created by wang007 on 2025/9/27
 */
public class BCDNumberDeserializer extends AbstractNumberUnitDeserializer<BCDNumber> {

    public static final BCDNumberDeserializer INSTANCE = new BCDNumberDeserializer(null);

    protected BCDNumberDeserializer(FrameField frameField) {
        super(frameField);
    }

    @Override
    public BCDNumber deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
        FrameField frameField = this.frameField;
        double value = p.getDoubleValue();
        return BCDNumber.from(value, frameField.unit(), frameField.len());
    }

    @Override
    protected JsonDeserializer<?> createSelf(FrameField frameField) {
        return new BCDNumberDeserializer(frameField);
    }
}
